package util;

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import on.Tarjeta;

public class Validador {
	private static Logger __log = Logger.getLogger(Validador.class.toString());

	public static boolean isNifNie(String nif) {

		__log.info("NIF " + nif);
		// si es NIE, eliminar la x,y,z inicial para tratarlo como nif
		if (nif.toUpperCase().startsWith("X")
				|| nif.toUpperCase().startsWith("Y")
				|| nif.toUpperCase().startsWith("Z"))
			nif = nif.substring(1);

		Pattern nifPattern = Pattern
				.compile("(\\d{1,8})([TRWAGMYFPDXBNJZSQVHLCKEtrwagmyfpdxbnjzsqvhlcke])");
		Matcher m = nifPattern.matcher(nif);
		if (m.matches()) {
			String letra = m.group(2);
			// Extraer letra del NIF
			String letras = "TRWAGMYFPDXBNJZSQVHLCKE";
			int dni = Integer.parseInt(m.group(1));
			dni = dni % 23;
			String reference = letras.substring(dni, dni + 1);

			if (reference.equalsIgnoreCase(letra)) {
				__log.info("son iguales. Es NIF. " + letra + " " + reference);
				return true;
			} else {
				__log.info("NO son iguales. NO es NIF. " + letra + " "
						+ reference);
				return false;
			}
		} else
			return false;
	}

	public static String filtrarCaracteresRaros(String texto) {
		return texto.replaceAll("[^a-zA-Z0-9._%-]", " ");// [A-Z0-9._%-]
	}

	public static boolean validarMail(String mail) {
		Pattern mailPattern = Pattern
				.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
						+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
		Matcher matcher = mailPattern.matcher(mail);
		return matcher.matches();
	}

	public static boolean TarjetaActiva(Tarjeta tar) {
		boolean correcto = false;
		if (tar.getFechaBaja() == null)
			correcto = true;
		return correcto;

	}

	public static boolean SoloNumeros(char c) {
		String test = String.valueOf(c);
		return test.matches("[0-9.]");
	}

}
